package com.jetpackframework.retrofit.converter;

import androidx.annotation.Nullable;

import com.gwm.annotation.http.Query;
import com.jetpackframework.retrofit.Converter;
import com.zhy.http.okhttp.builder.GetBuilder;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class GetBuilderRequestConverter extends HttpRequestConverter<GetBuilder> {
    private static Converter converter;
    public static synchronized Converter get(){
        if (converter == null){
            converter = new GetBuilderRequestConverter();
        }
        return converter;
    }
    private GetBuilderRequestConverter(){}
    @Nullable
    @Override
    public GetBuilder convert(Method method,Object[] args){
        GetBuilder builder = new GetBuilder();
        initHttpBasicInfo(method,args, builder);
        Annotation[][] annotations = method.getParameterAnnotations();
        for (int i = 0; i < annotations.length; i++) {     //i:第几个参数的注解
            if (annotations[i].length > 0) {
                for (int j = 0; j < annotations[i].length; j++) {  //j: 第i个参数上的第j个注解
                    if(annotations[i][j] instanceof Query) {
                        Query query = (Query) annotations[i][j];
                        String key = query.value();
                        Object value = args[i];
                        builder.addParams(key,String.valueOf(value));
                    }
                }
            }
        }
        return builder;
    }

}
